﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Compiler
{
    public class EbnfRuleSequence : EbnfNode
    {
        EbnfNode first;
        EbnfNode rest;

        public EbnfRuleSequence(EbnfNode first, EbnfNode rest)
        {
            this.first = first;
            if (!(rest is EbnfEmptyNode))
                this.rest = rest;
        }

        public override void Convert2Grammar(Ebnf2GrammarConvertData data)
        {
            first.Convert2Grammar(data);
            if (rest != null && !(rest is EbnfEmptyNode))
                rest.Convert2Grammar(data);
        }

        public override EbnfNode Expand(Stack<EbnfAssign> Open)
        {
            first.Expand(Open);

            EbnfNode oldRest = rest;

            while (Open.Any())
            {
                EbnfAssign current = Open.Pop();
                current.Expand(Open);
                EbnfRuleSequence newRule = new EbnfRuleSequence(current, rest);
                rest = newRule;
            }

            //todo: war weiter oben. nachdenken ob das korrekt ist.
            if (oldRest != null)
                oldRest.Expand(Open);

            return this;
        }

        public override string ToString()
        {
            string s = first.ToString() + ";";
            if (rest != null)
                s += "\n" + rest.ToString();
            return s;
        }
    }
}
